package ipmss.entity.authorities;

import ipmss.entity.planning.Project;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;

import org.springframework.transaction.annotation.Transactional;

import net.sf.cglib.transform.impl.AddPropertyTransformer;

// TODO: Auto-generated Javadoc
/**
 * The Class User.
 *
 * @author Michał Czarnik
 */
@Entity
public class User
{
    
    /** The id. */
    @Id
    @GeneratedValue
    private int id;

    /** The login. */
    @Column(unique = true )
    private String login = "";
    
    /** The e mail. */
    @Column(unique = true)
    private String eMail = "";
    
    /** The password. */
    private String password = "";
    
    /** The salt. */
    private String salt = "";

    /** The profile. */
    @OneToOne(cascade = {CascadeType.ALL})
    private UserProfile profile;
    
    /** The settings. */
    @OneToOne(cascade = {CascadeType.ALL})
    private UserSettings settings;

    /** The group. */
    @ManyToOne(fetch = FetchType.LAZY,cascade = {CascadeType.ALL})
    private Group group;
    
    /** The projects. */
    @ManyToMany(fetch = FetchType.EAGER)
    private List<Project> projects = new ArrayList<Project>();

    /**
     * Gets the login.
     *
     * @return the login
     */
    public String getLogin()
    {
	return login;
    }
    
    /**
     * Sets the login.
     *
     * @param login the new login
     */
    public void setLogin(String login)
    {
	this.login = login;
    }

    /**
     * Gets the password.
     *
     * @return the password
     */
    public String getPassword()
    {
	return password;
    }

    /**
     * Sets the password.
     *
     * @param password the new password
     */
    public void setPassword(String password)
    {
	this.password = password;
    }

    /**
     * Gets the salt.
     *
     * @return the salt
     */
    public String getSalt()
    {
	return salt;
    }

    /**
     * Sets the salt.
     *
     * @param salt the new salt
     */
    public void setSalt(String salt)
    {
	this.salt = salt;
    }

    /**
     * Gets the profile.
     *
     * @return the profile
     */
    public UserProfile getProfile()
    {
	return profile;
    }

    /**
     * Sets the profile.
     *
     * @param profile the new profile
     */
    public void setProfile(UserProfile profile)
    {
	this.profile = profile;
    }

    /**
     * Gets the group.
     *
     * @return the group
     */
    public Group getGroup()
    {
	return group;
    }

    /**
     * Sets the group.
     *
     * @param group the new group
     */
    public void setGroup(Group group)
    {
	this.group = group;
    }

    /**
     * Gets the id.
     *
     * @return the id
     */
    public int getId()
    {
	return id;
    }

    /**
     * Sets the id.
     *
     * @param id the new id
     */
    public void setId(int id)
    {
	this.id = id;
    }

    /**
     * Gets the e mail.
     *
     * @return the e mail
     */
    public String geteMail()
    {
	return eMail;
    }

    /**
     * Sets the e mail.
     *
     * @param eMail the new e mail
     */
    public void seteMail(String eMail)
    {
	this.eMail = eMail;
    }

    /**
     * Gets the projects.
     *
     * @return the projects
     */
    public List<Project> getProjects()
    {
	return projects;
    }

    /**
     * Sets the projects.
     *
     * @param projects the new projects
     */
    public void setProjects(List<Project> projects)
    {
	this.projects = projects;
    }
    
    /**
     * Gets the settings.
     *
     * @return the settings
     */
    public UserSettings getSettings()
    {
	return settings;
    }
    
    /**
     * Sets the settings.
     *
     * @param settings the new settings
     */
    public void setSettings(UserSettings settings)
    {
	this.settings = settings;
    }

    /**
     * Adds the project.
     *
     * @param p the p
     */
    @Transactional
    public void addProject(Project p )
    {
	try
	{
	if(projects == null )
	    projects =  new ArrayList<Project>();
	projects.add(p);
	} catch (Exception e) {
	    projects =  new ArrayList<Project>();
	    projects.add(p);
	}
    }
    
}
